package com.hliushi.mq.base.producer;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.concurrent.TimeUnit;

/**
 * 发送单向消息
 *
 * @author llhuang10
 * @date 2021/8/11 17:03
 */
public class OneWayProducer {

    public static void main(String[] args) {
        DefaultMQProducer producer = new DefaultMQProducer("group1");
        producer.setNamesrvAddr("cvm-node01:9876;cvm-node02:9876");
        // 3.发送消息
        try {
            producer.start();
            for (int i = 0; i < 10; i++) {
                // 4.创建消息, 指定主题Topic, Tag和消费题
                Message message = new Message("base", "OneWay", ("hello world" + i).getBytes());

                // 发送单向消息
                producer.sendOneway(message);

                // 线程睡一秒
                TimeUnit.SECONDS.sleep(1);
            }
        } catch (MQClientException | InterruptedException | RemotingException e) {
            e.printStackTrace();
        } finally {
            // 6.关闭生产者producer
            System.out.println("发送成功!");
            producer.shutdown();
        }
    }
}
